package com.caimiao.miao.library.database.table;

import android.content.ContentValues;
import android.database.Cursor;

import com.caimiao.miao.bean.TicketIn;
import com.caimiao.miao.library.database.DBHelperImpl;

import java.util.ArrayList;
import java.util.List;

//入库
public class TicketInDBHelper extends DBHelperImpl<TicketIn> {

    @Override
    protected String getTableName() {
        return TicketInTable.TABLE_NAME;
    }

    public TicketInDBHelper() {
        super();
    }

    public synchronized int updateSync(TicketIn ticket) {
        ContentValues values = new ContentValues();
        values.put(TicketInTable.ATTR_SYNC, ticket.getSync());
        return update(values, TicketInTable._ID + " = ?", new String[]{String.valueOf(ticket.getId())});
    }

    public List<TicketIn> getNotSyncData() {
        List<TicketIn> dataList = new ArrayList<>();
        Cursor cursor = db.query(getTableName(), null,
                TicketInTable.ATTR_SYNC + " = ?", new String[]{"0"}, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                dataList.add(get(cursor));
            }
            cursor.close();
        }
        return dataList;
    }

    @Override
    public TicketIn get(Cursor cursor) {
        TicketIn data = new TicketIn();
        data.setId(cursor.getInt(cursor.getColumnIndex(TicketInTable._ID)));
        data.setType(cursor.getString(cursor.getColumnIndex(TicketInTable.ATTR_TYPE)));
        data.setCount(cursor.getInt(cursor.getColumnIndex(TicketInTable.ATTR_COUNT)));
        data.setDate(cursor.getString(cursor.getColumnIndex(TicketInTable.ATTR_DATE)));
        data.setDevice(cursor.getString(cursor.getColumnIndex(TicketInTable.ATTR_DEVICE)));
        data.setSync(cursor.getInt(cursor.getColumnIndex(TicketInTable.ATTR_SYNC)));
        return data;
    }

    @Override
    public ContentValues get(TicketIn data) {
        ContentValues values = new ContentValues();
        values.put(TicketInTable.ATTR_TYPE, data.getType());
        values.put(TicketInTable.ATTR_COUNT, data.getCount());
        values.put(TicketInTable.ATTR_DATE, data.getDate());
        values.put(TicketInTable.ATTR_DEVICE, data.getDevice());
        values.put(TicketInTable.ATTR_SYNC, data.getSync());
        return values;
    }
}
